package com.example.myapplication2;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class SQLiteDatabaseActivity extends AppCompatActivity implements View.OnClickListener {

    private String dataBaseName;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sqlite_database);
        findViewById(R.id.createDataBase).setOnClickListener(this);
        findViewById(R.id.deleteDataBase).setOnClickListener(this);
        textView = findViewById(R.id.DataBaseTextView);
        dataBaseName = "test.db";
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            //创建
            case R.id.createDataBase:
                //打开或者创建数据库,其中factory,指数据库结构
                SQLiteDatabase sqLiteDatabase = openOrCreateDatabase(dataBaseName, Context.MODE_PRIVATE, null);
                textView.setText(String.format("数据库地址%s创建%s",sqLiteDatabase.getPath(),(sqLiteDatabase!=null)?"成功":"失败"));
                break;
                //删除
            case R.id.deleteDataBase:
                //删除
                boolean b = deleteDatabase(dataBaseName);
                textView.setText(String.format("数据库地址%s创建%s",dataBaseName,(b)?"成功":"失败"));
                break;
        }
    }
}